package com.yehuishou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.BidItem;
import com.yehuishou.pojo.Bidrecord;
import com.yehuishou.pojo.User;
import com.yehuishou.service.BidItemService;
import com.yehuishou.service.BidRecordService;

/**
 * 
 * @author ChicUniqueKing(肖洒)
 * @className  竞价控制器
 */
 @Controller
 public class BidItemController {
	
	 
	 private Map<String, Object> map = new HashMap<>();
	 
	 @Autowired
	 private BidRecordService bidRecordService;
	
	
	 @Autowired
	 private BidItemService bidItemService;
	 
	 //用户进入竞价商品的页面
	 @RequestMapping("/tobid")
	 public String toBid(String npage,HttpServletRequest req) {
		
		 Integer page =1;
		 if(npage!=null&&npage!="") {
			page =Integer.parseInt(npage);
		 }
		 
		 PageHelper.startPage(page, 9);
		 List<BidItem> bids = bidItemService.getAll();
		 PageInfo<BidItem> pageInfo = new PageInfo<>(bids);
		 req.setAttribute("pageinfo", pageInfo);
		 return "biditem";
	 }

	 //用户竞价
	 @ResponseBody
	 @RequestMapping("/bidcheck")
	 public Map<String, Object> tobidPrice(HttpServletRequest req,Integer bid) {
		
		 //获取session
		HttpSession session =req.getSession(false);
	// 定义返回值
	 boolean flag =false;
		//判断用户是否登录session是否为空
	 User user=null;
	  if(session!=null) {
			 //判断是否为竞价商
		 user = (User) session.getAttribute("user");
	  }
		if(user!=null) {
			//用户是竞价商
			if(user.getState().equals(3)) {
			BidItem bi = bidItemService.getOne(bid);
			 //判断用户是否有出价记录
			 Bidrecord re = new Bidrecord();
			 re.setBid(bid);
			 re.setUid(user.getUid());
			 re.setAddtime(bi.getPublish_time());
			 re.setUpdatetime(bi.getDelivery_time());
			 //如果有竞价记录
			if(bidRecordService.selectRecordBybid(re).size()>0) {
				flag=false;//错误的提示页面 您已经参与过一次竞价的 
				map.put("msg", "您已经参加过一次竞价");
			}else {
				 flag =true;
			 } 
	   }else {
		   map.put("msg","请您联系客服成为竞价商");// 进入成为竞价商的页面
		 } 
		//没有登录
		 }else {
			 map.put("msg","您还没有登录");//进入个人信息页面
	 
	 }
		  map.put("flag", flag);
        return map;
 }
	 
	 
	 //竞价商进入
	 @RequestMapping("/bid")
	 public String tobid(HttpServletRequest req,Integer bid) {
		 
	 BidItem bids = bidItemService.getOne(bid);
		
	 req.setAttribute("bid",bids);
		 return "biding";//竞价商出价页面
		 
	 }
	//用户提交竞价信息
  @ResponseBody
  @RequestMapping("/bid_s")
  public Map<String, Object> bidPrice(Bidrecord re,HttpSession session) {
	     //插入竞价记录
	  boolean flag =false;
	  if(session!=null) {
		  User user=(User)session.getAttribute("user");
			if(user!=null) {
				re.setUid(user.getUid());
			}  
	  }
	 
	     //防止用户刷新页面重复出价
        if(bidRecordService.selectRecordBybid(re).size()>0) {
        
	  }else {
			 re.setRe_status(1);
	      	  bidRecordService.insertBidRecord(re);
		  flag =true;
		  
	  }
        
	map.put("flag", flag);
	 return map;
  }
  
    
  @ResponseBody
  @RequestMapping("/bbbb")
  public Map<String, Object> bbbbbbbb(HttpServletRequest req) {
	  
	 HttpSession session =req.getSession(false);
	  
	  
	  User user = (User)session.getAttribute("user");
    if(user!=null) {
    	
    	Integer uid = user.getUid();
    	List<Bidrecord>  list= bidRecordService.selectRecordByUid(uid);

          map.put("list", list);
    	
    }
   
    return map;
    
   } 
  
 
  //改变所有该竞价商品的竞价记录状态为结束
  @ResponseBody
  @RequestMapping("/changeRecordStauts")
  public boolean changeRecordStauts(HttpServletRequest req,Integer bid) {
	  Bidrecord br = new Bidrecord();
	  BidItem bi = new BidItem();
	  br.setBid(bid);
	  br.setRe_status(2);//竞价结束
	  bidRecordService.changeRecordStauts(br);
	  bi.setBid(bid);
	  bi.setStatus(2);
	  bidItemService.changBidStatus(bi);//修改竞价商品的状态为结束
	  boolean flag = true;
      return flag;
   } 
  
  
  
/*  
  
  @RequestMapping("/bid_sus")
  public String bidSuccess(Integer bid,HttpServletRequest req) {
	   
	  Bidrecord re =  new Bidrecord();
	  HttpSession session = req.getSession(false);
	  
	  if(session!=null) {
		   User user =(User)session.getAttribute("user");
		   if(user!=null) {
		   re.setUid(user.getUid());
		   }
	   }
	    re.setBid(bid);
	    Bidrecord re1 = new Bidrecord();
	    re1= bidRecordService.getBidRecord(re).get(0);
	    req.setAttribute("bidre", re1);
	  return "biding_success";
    }*/
  }
